#include "interface.h"
#include "world.h"

namespace sleek
{
    namespace core
    {
        namespace engine
        {
            interface::interface(world *w) : everythink(w), screen(w->getDevice()), mom(w->getDrawManager())
            {
                cr = new gui::cursor(this);
                internal = new gui::font(0,0);
                interne = new gui::theme(this);
                custom = interne;
            }
            interface::~interface()
            {
                clear();
                delete cr;
            }
            gui::frame* interface::addFrame(core::os::stringc t, math::aabbox2di b)
            {
                gui::frame *tmp = new gui::frame(this);
                tmp->absolute = b.minimise();
                tmp->box = b;
                tmp->setText(t);
                return tmp;
            }
            gui::picture* interface::addPicture(texture::texture *w, os::stringc t, math::aabbox2di b)
            {
                gui::picture *tmp = new gui::picture(this);
                tmp->absolute = b.minimise();
                tmp->box = b;
                tmp->setText(t);
                tmp->setTexture(w);
                return tmp;
            }
            gui::progressbar* interface::addProgressbar(os::stringc t, math::aabbox2di b)
            {
                gui::progressbar *tmp = new gui::progressbar(this);
                tmp->absolute = b.minimise();
                tmp->box = b;
                tmp->setText(t);
                return tmp;
            }
            gui::button* interface::addButton(core::os::stringc t, math::aabbox2di b)
            {
                gui::button *tmp = new gui::button(this);
                tmp->absolute = b.minimise();
                tmp->box = b;
                tmp->setText(t);
                return tmp;
            }
            gui::checkbox* interface::addCheckBox(core::os::stringc t, math::aabbox2di b)
            {
                gui::checkbox *tmp = new gui::checkbox(this);
                tmp->absolute = b.minimise();
                tmp->box = b;
                tmp->setText(t);
                return tmp;
            }
            gui::statictext* interface::addStaticText(core::os::stringc t, math::aabbox2di b)
            {
                gui::statictext *tmp = new gui::statictext(this);
                tmp->absolute = b.minimise();
                tmp->box = b;
                tmp->setText(t);
                return tmp;
            }
            gui::editbox* interface::addEditBox(core::os::stringc t, math::aabbox2di b)
            {
                gui::editbox *tmp = new gui::editbox(this);
                tmp->absolute = b.minimise();
                tmp->box = b;
                tmp->setText(t);
                return tmp;
            }
            gui::window* interface::addWindow(core::os::stringc t, math::aabbox2di b)
            {
                gui::window *tmp = new gui::window(this);
                tmp->absolute = b.minimise();
                tmp->text = t;
                tmp->box = b;
                return tmp;
            }
            void interface::addCustomFrame(gui::frame *tmp)
            {
                tmp->move(tmp->absolute);
                gui.push_back(tmp);
            }
            /** ******************************************** **/
            gui::theme* interface::getTheme()
            {
                return custom;
            }
            gui::font* interface::getInternalFont()
            {
                return internal;
            }
            gui::font* interface::getFont(os::stringc i)
            {
                return new gui::font(i);
            }
            void interface::unActiveElement()
            {
                for(core::u32 a = 0; a<gui.size(); ++a)
                    gui[a]->isActive = false;
            }
            bool interface::manage(core::device::input *a)
            {
                if(!a) return false;
                cr->manage(a);
                if(a->type == device::EVENT_MOUSSE_UP && cr) cr->grab();
                for(core::u32 i = 0; i<gui.size(); ++i)
                {
                    if(gui[gui.size()-1-i]->manage(a))
                    {
                        return true;
                    }
                }
                return false;
            }
            void interface::popFrame(gui::frame *i)
            {
                int x = gui.linear_search(i);
                if(x == -1) return;
                popFrame(x);
            }
            void interface::ActiveElement(gui::frame *i)
            {
                unActiveElement();
                popFrame(i);
                i->isActive = true;
            }
            void interface::removeFrame(gui::frame *i)
            {
                int x = gui.linear_search(i);
                if(x == -1) return;
                removeFrame(x);
            }
            void interface::removeFrame(s32 i)
            {
                delete gui[i];
                gui.erase(i);
            }
            void interface::popFrame(s32 i)
            {
                gui::frame *tmp = gui[i];
                gui.erase(i);
                gui.push_back(tmp);
            }
            void interface::clear()
            {
                while(gui.size()) removeFrame(0);
            }
            texture::draw* interface::getDrawManager()
            {
                return mom;
            }
            device::Device* interface::getDevice()
            {
                return screen;
            }
            gui::cursor *interface::getCursor()
            {
                return cr;
            }
            world* interface::getWorld()
            {
                return everythink;
            }
            void interface::render()
            {
                glPushMatrix();
                    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
                    for(core::u32 i = 0; i<gui.size(); ++i)
                        gui[i]->render();
                    cr->render();
                glPopMatrix();
            }
        }
    }
}
